﻿using DC2010.Objects.Common;
using DC2010.Objects.Data;
using DC2010.Objects.Logic;

namespace DC2010.Objects
{
    public class TileLogic : BaseLogic
    {


        // 0  1  2  3  4
        //[ ][ ][ ][ ][ ]  4
        //[ ][ ][ ][ ][ ]  3
        //[ ][ ][ ][ ][ ]  2
        //   [ ][ ][ ]     1
        //   [ ][x][ ]     0     

        public TileInfo Info { get; set; }
        public TileGfx Gfx { get; set; }

        public TileLogic(TileData td)
        {
            Info = new TileInfo();
            Gfx = Root.I.TileSystem.GetGfx(td.GfxId);
            Data = td;

        }

        public void PartyEnter(PartyData p)
        {
            foreach (KeyValue key in Info.OnEnterEffects)
            {
                EffectData ef = Root.I.EffectSystem.GetData(key.Value);
                if (ef != null)
                {
                    //who am I affecting - Party
                    ef.L.Instant(this, p);
                }
            }
        }

        public void PartyLeave(PartyData p)
        {
            foreach (KeyValue key in Info.OnLeaveEffects)
            {
                EffectData ef = Root.I.EffectSystem.GetData(key.Value);
                if (ef != null)
                {
                    //who am I affecting - Party
                    ef.L.Instant(this, p);
                }
            }
        }

        /// <summary>
        /// Checks if party can enter this tile
        /// </summary>
        /// <param name="c">Creature</param>
        ///
        public virtual bool EnterRequest(PartyData c)
        {
            if (c.IsMonsterParty)
            {
                return Info.CanMonsterPass;
            }

            return true;

            //return Info.CanPlayerPass;
        }

        public PartyData ContainsParty()
        {
            TileData td = (TileData)Data;
            foreach (PartyData pd in Root.I.PartySystem.DictData.Values)
            {
                if (td.X == pd.Location.X)
                {
                    if (td.Y == pd.Location.Y)
                    {
                        return pd;
                    }
                }
            }

            return null;
        }
    }
}
